package com.common.platform.sys.modular.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.platform.auth.annotation.Permission;
import com.common.platform.auth.context.LoginContextHolder;
import com.common.platform.base.config.database.DataScope;
import com.common.platform.base.config.sys.Const;
import com.common.platform.base.log.BussinessLog;
import com.common.platform.base.page.LayuiPageInfo;
import com.common.platform.base.page.factory.LayuiPageFactory;
import com.common.platform.base.utils.CoreUtil;
import com.common.platform.sys.base.controller.BaseController;
import com.common.platform.sys.base.controller.response.ResponseData;
import com.common.platform.sys.dictmap.DeptDict;
import com.common.platform.sys.exception.RequestEmptyException;
import com.common.platform.sys.factory.ConstantFactory;
import com.common.platform.sys.log.LogObjectHolder;
import com.common.platform.sys.modular.system.entity.Dept;
import com.common.platform.sys.modular.system.entity.SysNotice;
import com.common.platform.sys.modular.system.service.MenuService;
import com.common.platform.sys.modular.system.service.SysNoticeService;
import com.common.platform.sys.modular.system.wrapper.MenuWrapper;
import com.common.platform.sys.modular.system.wrapper.UserWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController {
    private static String PREFIX = "/modular/system/notice/";
    @Autowired
    private SysNoticeService sysNoticeService;

    /**
     * 跳转到菜单首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "notice.html";
    }

    /**
     * 跳转到菜单添加页面
     */
    @RequestMapping("/notice_add")
    public String noticeAdd() {
        return PREFIX + "notice_add.html";
    }

    /**
     * 查询人员列表
     */
//    @Permission
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(value = "condition", required = false) String condition) {
        List<SysNotice> list = new ArrayList<>();
        if (CoreUtil.isEmpty(condition)) {
            list = this.sysNoticeService.list();
        } else {
            list = this.sysNoticeService.list(new LambdaQueryWrapper<SysNotice>()
                    .like(SysNotice::getTitle, condition).or().like(SysNotice::getContent, condition));
        }
        LayuiPageInfo result = new LayuiPageInfo();
        result.setData(list);
        return result;

    }

    /**
     * 跳转到修改部门
     */
    @RequestMapping("/notice_update/{noticeId}")
    public String noticeUpdate(@PathVariable("noticeId") Long noticeId) {

        SysNotice dept = this.sysNoticeService.getById(noticeId);
        LogObjectHolder.me().set(dept);

        return PREFIX + "notice_edit.html";
    }

    @BussinessLog(value = "添加通知", key = "simpleName", dict = DeptDict.class)
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(SysNotice notice) {
        notice.setCreateUser(LoginContextHolder.getContext().getUserId());
        notice.setCreateTime(new Date());
        this.sysNoticeService.save(notice);
        return SUCCESS_TIP;
    }

    @BussinessLog(value = "修改部门", key = "simpleName", dict = DeptDict.class)
    @RequestMapping("/update")
    @ResponseBody
    public ResponseData update(SysNotice dept) {
        this.sysNoticeService.updateById(dept);
        return SUCCESS_TIP;
    }

    @BussinessLog(value = "删除", key = "simpleName", dict = DeptDict.class)
    @PostMapping("/delete")
    @ResponseBody
    public ResponseData delete(@RequestParam("noticeId") Long noticeId) {
        this.sysNoticeService.removeById(noticeId);
        return SUCCESS_TIP;
    }
}
